﻿Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Imports SecretaryToolData

Partial Public Class Reports

	Public Shared Function PublisherRecordCardHeader(PublisherID As Long, Optional OutputFile As FileInfo = Nothing) As FileInfo
		Dim workingPublisher As New VM_PublisherInformation(PublisherID)
		Dim workingEmergencyContactInfo As VM_EmergencyContactInformation
		If workingPublisher.FamilyHeadID Is Nothing Then
			workingEmergencyContactInfo = New VM_EmergencyContactInformation(PublisherID)
		Else
			workingEmergencyContactInfo = New VM_EmergencyContactInformation(workingPublisher.FamilyHeadID)
		End If

		If OutputFile Is Nothing Then
			OutputFile = New FileInfo(String.Format("{0}\{1}-{2}.{3}", ConfigPropertyKeys.AutoBackupLocation, "PublisherCardHeader", workingPublisher.FullName.AppendFileDateExtension, "pdf"))
		End If

		Dim BlackBaseColor As New BaseColor(Color.Black)
		Dim fontItem As New Font(UniCodeBaseFont, 50, Font.NORMAL, BlackBaseColor)

		Using myFileStream As New FileStream(OutputFile.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite)
			'72 pixels per inch, 0 margins
			Using myDoc As New Document(New Rectangle(6 * 72, 4 * 72), 0, 0, 0, 0)
				Dim writer As PdfWriter = PdfWriter.GetInstance(myDoc, myFileStream)

				myDoc.Open()

				Dim content As PdfContentByte = writer.DirectContent
				Dim name As String
				name = workingPublisher.FullName

				Dim NameSpecs As New FieldSpecs
				Dim HomeAddressSpecs As New FieldSpecs
				Dim HomeTelephoneSpecs As New FieldSpecs
				Dim MobileTelephoneSpecs As New FieldSpecs
				Dim DateOfBirthSpecs As New FieldSpecs
				Dim DateImmersedSpecs As New FieldSpecs
				Dim AnointedSpecs As New FieldSpecs
				Dim ElderSpecs As New FieldSpecs
				Dim MinisterialServantSpecs As New FieldSpecs
				Dim RegularPioneerSpecs As New FieldSpecs
				Dim MaleSpecs As New FieldSpecs
				Dim FemaleSpecs As New FieldSpecs

				Select Case ConfigPropertyKeys.S21Language
					Case "E"
						NameSpecs = New FieldSpecs(40, Nothing, 342, Nothing)
						HomeAddressSpecs = New FieldSpecs(67, Nothing, 348, Nothing)
						HomeTelephoneSpecs = New FieldSpecs(74, Nothing, 78, Nothing)
						MobileTelephoneSpecs = New FieldSpecs(213, Nothing, 78, Nothing)
						DateOfBirthSpecs = New FieldSpecs(336, Nothing, 78, Nothing)
						DateImmersedSpecs = New FieldSpecs(70, Nothing, 100, Nothing)
						AnointedSpecs = New FieldSpecs(269, Nothing, 41, Nothing)
						ElderSpecs = New FieldSpecs(316, Nothing, 6, Nothing)
						MinisterialServantSpecs = New FieldSpecs(346, Nothing, 6, Nothing)
						RegularPioneerSpecs = New FieldSpecs(316, Nothing, 6, Nothing)
						MaleSpecs = New FieldSpecs(408, 260, 6, 6)
						FemaleSpecs = New FieldSpecs(408, 250, 6, 6)
					Case "CH"
						NameSpecs = New FieldSpecs(40, Nothing, 342, Nothing)
						HomeAddressSpecs = New FieldSpecs(40, Nothing, 408, Nothing)
						HomeTelephoneSpecs = New FieldSpecs(50, Nothing, 101, Nothing)
						MobileTelephoneSpecs = New FieldSpecs(185, Nothing, 101, Nothing)
						DateOfBirthSpecs = New FieldSpecs(316, Nothing, 101, Nothing)
						DateImmersedSpecs = New FieldSpecs(50, Nothing, 140, Nothing)
						AnointedSpecs = New FieldSpecs(269, Nothing, 67, Nothing)
						ElderSpecs = New FieldSpecs(340, Nothing, 6, Nothing)
						MinisterialServantSpecs = New FieldSpecs(375, Nothing, 6, Nothing)
						RegularPioneerSpecs = New FieldSpecs(340, Nothing, 6, Nothing)
						MaleSpecs = New FieldSpecs(408, 265, 6, 6)
						FemaleSpecs = New FieldSpecs(408, 255, 6, 6)
				End Select

				PlaceText(content, name, fontItem, NameSpecs, 242, 10, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, If(workingPublisher.IsMale, "█", ""), fontItem, MaleSpecs, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, If(workingPublisher.IsMale, "", "█"), fontItem, FemaleSpecs, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, workingEmergencyContactInfo.PhysicalAddress, fontItem, HomeAddressSpecs, 229, 10, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, workingEmergencyContactInfo.HomeTelephone, fontItem, HomeTelephoneSpecs, 216, 10, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, workingPublisher.MobileTelephone, fontItem, MobileTelephoneSpecs, 216, 10, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, If(workingPublisher.BirthDate > DateTime.MinValue, workingPublisher.BirthDate.Value.ToString("d MMM yyyy"), ""), fontItem, DateOfBirthSpecs, 216, 10, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, If(workingPublisher.ImmersionDate IsNot Nothing, workingPublisher.ImmersionDate.Value.ToString("d MMM yyyy"), ""), fontItem, DateImmersedSpecs, 195, 13, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, If(workingPublisher.IsAnointed, "Anointed", "other sheep"), fontItem, AnointedSpecs, 195, 13, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, If(workingPublisher.IsElder, "█", ""), fontItem, ElderSpecs, 210, 6, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, If(workingPublisher.IsMinisterialServant, "█", ""), fontItem, MinisterialServantSpecs, 210, 6, 0.75, Element.ALIGN_LEFT)
				PlaceText(content, If(workingPublisher.IsPioneer, "█", ""), fontItem, RegularPioneerSpecs, 200, 6, 0.75, Element.ALIGN_LEFT)

				'marks to position cart on page for testing
				'PlaceText(content, "|", fontItem, 0, 0, 10, 1, 0.75, Element.ALIGN_LEFT)
				'PlaceText(content, "|", fontItem, 422, 287, 10, 1, 0.75, Element.ALIGN_RIGHT)
				'PlaceText(content, "|", fontItem, 0, 287, 10, 1, 0.75, Element.ALIGN_LEFT)
				'PlaceText(content, "|", fontItem, 422, 0, 10, 1, 0.75, Element.ALIGN_RIGHT)

			End Using
		End Using

		Return OutputFile

	End Function

	Public Shared Function PublisherRecordCardHeader(PublisherIDs As List(Of Long)) As List(Of FileInfo)
		Dim outputFiles As New List(Of FileInfo)
		For Each PublisherID In PublisherIDs
			outputFiles.Add(PublisherRecordCardHeader(PublisherID))
		Next
		Return outputFiles
	End Function

	Public Shared Function PublisherRecordCard(PublisherID As Long, Dates As List(Of DateTime), Optional IncludeHeader As Boolean = False, Optional IncludeTotals As Boolean = False) As FileInfo
		Dim workingPublisher As New VM_PublisherInformation(PublisherID)
		'Dim workingPublisherData As New PublisherCardRecord(PublisherID, Dates)

		Dim HeaderFile As FileInfo = Nothing
		Dim TotalsFile As FileInfo = Nothing
		Dim OutputFile As New FileInfo(String.Format("{0}\{1}-{2}.{3}", ConfigPropertyKeys.AutoBackupLocation, "PublisherCard", workingPublisher.FullName.AppendFileDateExtension, "pdf"))

		Dim BlackBaseColor As New BaseColor(Color.Black)
		Dim fontItem As New Font(UniCodeBaseFont, 50, Font.NORMAL, BlackBaseColor)

		Using myFileStream As New FileStream(OutputFile.FullName, FileMode.Create, FileAccess.Write)
			'72 pixels per inch, 0 margins
			Using myDoc As New Document(New Rectangle(6 * 72, 4 * 72), 0, 0, 0, 0)
				Dim writer As PdfWriter = PdfWriter.GetInstance(myDoc, myFileStream)

				myDoc.Open()

				Dim content As PdfContentByte = writer.DirectContent

				For Each workingDate In Dates
					'Dim workingDate As DateTime = Dates(x)
					Dim record As New VM_PublisherCardRecord(PublisherID, workingDate.FirstOfTheMonth)
					Dim verticalOffset As Integer = -12 * (workingDate.Month - If(workingDate.Month > 8, 12, 0) + 3) + 162
					With record
						WritePublisherCardRecord(content, fontItem, verticalOffset, workingDate.ToString("\'yy"), .Books, .Brochures, .Hours, .Magazines, .ReturnVisits, .BibleStudies, .Remarks)
					End With
				Next

				If IncludeHeader Then
					HeaderFile = PublisherRecordCardHeader(PublisherID)
					Dim reader As New PdfReader(HeaderFile.FullName)
					Dim existingPage As PdfImportedPage = writer.GetImportedPage(reader, 1)
					content.AddTemplate(existingPage, 0, 0)
				End If

				If IncludeTotals Then
					TotalsFile = PublisherRecordCardTotals(PublisherID, Dates(0))
					Dim reader As New PdfReader(TotalsFile.FullName)
					Dim existingPage As PdfImportedPage = writer.GetImportedPage(reader, 1)
					content.AddTemplate(existingPage, 0, 0)
				End If

			End Using
		End Using

		If HeaderFile IsNot Nothing Then
			HeaderFile.Delete()
		End If

		If TotalsFile IsNot Nothing Then
			TotalsFile.Delete()
		End If

		Return OutputFile
	End Function

	Public Shared Function PublisherRecordCardTotals(PublisherID As Long, ServiceYearStartDate As DateTime) As FileInfo
		Dim workingPublisher As New VM_PublisherInformation(PublisherID)
		Dim workingPublisherCard As New VM_PublisherCard(PublisherID)
		'Dim workingPublisherData As New PublisherCardRecord(PublisherID, Dates)

		Dim OutputFile As New FileInfo(String.Format("{0}\{1}-{2}.{3}", ConfigPropertyKeys.AutoBackupLocation, "PublisherCardTotals", workingPublisher.FullName.AppendFileDateExtension, "pdf"))

		Dim BlackBaseColor As New BaseColor(Color.Black)
		Dim fontItem As New Font(UniCodeBaseFont, 50, Font.NORMAL, BlackBaseColor)

		Using myFileStream As New FileStream(OutputFile.FullName, FileMode.Create, FileAccess.Write)
			'72 pixels per inch, 0 margins
			Using myDoc As New Document(New Rectangle(6 * 72, 4 * 72), 0, 0, 0, 0)
				Dim writer As PdfWriter = PdfWriter.GetInstance(myDoc, myFileStream)

				myDoc.Open()

				Dim content As PdfContentByte = writer.DirectContent

				Dim verticalOffset As Integer = -12 * 12 + 162
				With workingPublisherCard
					WritePublisherCardRecord(content, fontItem, verticalOffset, "", .BooksForServiceYear(ServiceYearStartDate), .BrochuresForServiceYear(ServiceYearStartDate), .HoursForServiceYear(ServiceYearStartDate), .MagazinesForServiceYear(ServiceYearStartDate), .ReturnVisitsForServiceYear(ServiceYearStartDate), .BibleStudiesForServiceYear(ServiceYearStartDate), If(workingPublisher.IsPioneer, .PioneerHoursPaceForServiceYear(ServiceYearStartDate), ""))
				End With

			End Using
		End Using

		Return OutputFile
	End Function

	<Obsolete("This routine hasn't been tested.")>
	Public Shared Function PublisherRecordCard(PublisherIDs As List(Of Long)) As FileInfo

		Dim outputFile As New FileInfo(String.Format("{0}\{1}.{2}", ConfigPropertyKeys.AutoBackupLocation, "PublisherRecordCards".AppendFileDateExtension, "pdf"))

		Dim BlackBaseColor As New BaseColor(Color.Black)
		Dim fontPageTitle As New Font(UniCodeBaseFont, 28, Font.BOLD, BlackBaseColor)

		Dim filesToDelete As New List(Of FileInfo)

		Using myFileStream As New FileStream(outputFile.FullName, FileMode.Create)
			Using myDoc As New Document
				Dim writer As PdfWriter = PdfWriter.GetInstance(myDoc, myFileStream)

				myDoc.Open()

				Dim content As PdfContentByte = writer.DirectContent

				myDoc.Add(New Phrase("Congregation Emergency Contact Forms", fontPageTitle))

				For x As Integer = 0 To PublisherIDs.Count - 1
					Dim FamilyHeadPublisherID As Long = PublisherIDs(x)
					Dim EmergencyContactInformationFile As FileInfo = EmergencyContactInformation(FamilyHeadPublisherID)
					filesToDelete.Add(EmergencyContactInformationFile)
					Dim reader As New PdfReader(EmergencyContactInformationFile.FullName)
					myDoc.NewPage()
					Dim existingPage As PdfImportedPage = writer.GetImportedPage(reader, 1)
					content.AddTemplate(existingPage, 0, 0)
				Next
			End Using
		End Using

		For Each fileToDelete In filesToDelete
			fileToDelete.Delete()
		Next

		Return outputFile
	End Function

	Private Shared Sub WritePublisherCardRecord(ByRef content As PdfContentByte, font As Font, verticalOffset As Integer, Year As String, Books As String, Brochures As String, Hours As String, Magazines As String, ReturnVisits As String, BibleStudies As String, Remarks As String)

		'we aren't supposed to print 0's on publisher cards
		If Books = 0 Then Books = ""
		If Brochures = 0 Then Brochures = ""
		If Hours = 0 Then Hours = ""
		If Magazines = 0 Then Magazines = ""
		If ReturnVisits = 0 Then ReturnVisits = ""
		If BibleStudies = 0 Then BibleStudies = ""

		Dim YearSpecs As New FieldSpecs
		Dim BooksSpecs As New FieldSpecs
		Dim BrochuresSpecs As New FieldSpecs
		Dim HoursSpecs As New FieldSpecs
		Dim MagazinesSpecs As New FieldSpecs
		Dim ReturnVisitsSpecs As New FieldSpecs
		Dim BibleStudiesSpecs As New FieldSpecs
		Dim RemarksSpecs As New FieldSpecs

		Select Case ConfigPropertyKeys.S21Language
			Case "E"
				YearSpecs = New FieldSpecs(39, Nothing, 11, Nothing)
				BooksSpecs = New FieldSpecs(60, Nothing, 20, Nothing)
				BrochuresSpecs = New FieldSpecs(95, Nothing, 20, Nothing)
				HoursSpecs = New FieldSpecs(134, Nothing, 25, Nothing)
				MagazinesSpecs = New FieldSpecs(174, Nothing, 20, Nothing)
				ReturnVisitsSpecs = New FieldSpecs(209, Nothing, 20, Nothing)
				BibleStudiesSpecs = New FieldSpecs(243, Nothing, 20, Nothing)
				RemarksSpecs = New FieldSpecs(273, Nothing, 138, Nothing)
			Case "CH"
				YearSpecs = New FieldSpecs(37, Nothing, 11, Nothing)
				BooksSpecs = New FieldSpecs(55, Nothing, 20, Nothing)
				BrochuresSpecs = New FieldSpecs(85, Nothing, 20, Nothing)
				HoursSpecs = New FieldSpecs(114, Nothing, 25, Nothing)
				MagazinesSpecs = New FieldSpecs(147, Nothing, 20, Nothing)
				ReturnVisitsSpecs = New FieldSpecs(178, Nothing, 20, Nothing)
				BibleStudiesSpecs = New FieldSpecs(210, Nothing, 20, Nothing)
				RemarksSpecs = New FieldSpecs(239, Nothing, 176, Nothing)
		End Select

		PlaceText(content, Year, font, YearSpecs, verticalOffset, 11, 0.75, Element.ALIGN_RIGHT)
		PlaceText(content, Books, font, BooksSpecs, verticalOffset, 11, 0.75, Element.ALIGN_CENTER)
		PlaceText(content, Brochures, font, BrochuresSpecs, verticalOffset, 11, 0.75, Element.ALIGN_CENTER)
		PlaceText(content, Hours, font, HoursSpecs, verticalOffset, 11, 0.75, Element.ALIGN_CENTER)
		PlaceText(content, Magazines, font, MagazinesSpecs, verticalOffset, 11, 0.75, Element.ALIGN_CENTER)
		PlaceText(content, ReturnVisits, font, ReturnVisitsSpecs, verticalOffset, 11, 0.75, Element.ALIGN_CENTER)
		PlaceText(content, BibleStudies, font, BibleStudiesSpecs, verticalOffset, 11, 0.75, Element.ALIGN_CENTER)
		PlaceText(content, Remarks, font, RemarksSpecs, verticalOffset, 11, 0.75, Element.ALIGN_LEFT)
	End Sub
End Class
